import { GameMode } from "@minecraft/server";
import { PlayerGroup } from "@sapi-game/gamePlayer/playerGroup";
import { AutoStopState } from "@sapi-game/gameState/common/autoStop";
import { CvSConfig, CvSContext } from "./context";
import { CvSMainState } from "./mainState";
import { ChickenVsShepherdModule } from "./module";

export class CvSGame extends ChickenVsShepherdModule.Engine<{
    chicken: PlayerGroup;
    shepherd: PlayerGroup;
    config: CvSConfig;
}> {
    protected override buildContext(config: {
        chicken: PlayerGroup;
        shepherd: PlayerGroup;
        config: CvSConfig;
    }): CvSContext {
        const chicken = this.groupBuilder.fromGroup(config.chicken);
        const shepherd = this.groupBuilder.fromGroup(config.shepherd);
        return new CvSContext(chicken, shepherd, config.config);
    }
    override onStart(): void {
        this.pushState(CvSMainState).pushState(AutoStopState, {
            groupSet: this.context.groupSet,
            onLeave(p) {
                this.groupSet.sendMessage(`${p.name} 离开了小鸡大战牧人`);
            },
        });
    }

    override onStop(): void {
        this.context.groupSet.forEach((p) => {
            p.player.teleport(
                { x: 34.5, y: -60.0, z: 52.5 },
                { rotation: { x: 0, y: 0 } }
            );
            p.clear();
            p.player.setGameMode(GameMode.Adventure);
            p.player.runCommand("effect @s clear");
            p.player.setSpawnPoint(); //清空重生点
        });
    }
}
